package com.zzdz.jdbc.common;

import com.zzdz.jdbc.common.exception.CustomRuntimeException;
import com.zzdz.jdbc.common.exception.enums.ExceptionCodeEnum;

import java.io.IOException;
import java.sql.*;
import java.util.Properties;

/**
 * <p>
 * JavaDataBaseConnectivityConnectionFactory<br>
 * JDBC 连接工厂
 * </p>
 *
 * @author XinLau
 * @version 1.0
 * @since 2019年10月16日 14:10
 */
public class JavaDataBaseConnectivityConnectionFactory {

    /**
     * 数据库类型
     * 0 - MySQL
     * 1 - Oracle
     */
    private static int DATA_BASE = 0;

    /**
     * Driver
     */
    private static String driver;

    /**
     * url
     */
    private static String url;

    /**
     * userAccount
     */
    private static String userAccount;

    /**
     * userPassword
     */
    private static String userPassword;

    static{
        try {
            Properties properties = new Properties();
            properties.load(JavaDataBaseConnectivityConnectionFactory.class.getClassLoader().getResourceAsStream("JavaDataBaseConnectivity.properties"));
            switch (DATA_BASE){
                case 1 :
                    driver = properties.getProperty("driverForOracle");
                    url = properties.getProperty("urlForOracle");
                    userAccount = properties.getProperty("userAccountForOracle");
                    userPassword = properties.getProperty("userPasswordForOracle");
                    break;
                default :
                    driver = properties.getProperty("driverForMySQL");
                    url = properties.getProperty("urlForMySQL");
                    userAccount = properties.getProperty("userAccountForMySQL");
                    userPassword = properties.getProperty("userPasswordForMySQL");
            }
        } catch (IOException e) {
            throw new CustomRuntimeException(ExceptionCodeEnum.PROPERTIES_LOAD_EXCEPTION, e);
        }
    }

    /**
     * 开启连接
     * @return
     */
    public static Connection getConnection() {
        Connection connection = null;
        try {
            // 1.注册驱动
            Class.forName(driver);
            // 2.建立连接
            connection = DriverManager.getConnection(url, userAccount, userPassword);
        } catch (Exception e) {
            throw new CustomRuntimeException(ExceptionCodeEnum.CONNECTION_OPEN_EXCEPTION, e);
        }
        return connection;
    }

    /**
     * 提交
     * @param connection - 连接
     */
    public static void doCommit(Connection connection){
        try {
            connection.commit();
        } catch (SQLException e) {
            throw new CustomRuntimeException(ExceptionCodeEnum.CONNECTION_COMMIT_EXCEPTION, e);
        }
    }

    /**
     * 回滚
     * @param connection - 连接
     */
    public static void doRollback(Connection connection){
        try {
            connection.rollback();
        } catch (SQLException e) {
            throw new CustomRuntimeException(ExceptionCodeEnum.CONNECTION_ROLLBACK_EXCEPTION, e);
        }
    }

    /**
     * 关闭连接
     * @param connection - 连接
     * @param statement -
     */
    public static void doClose(Connection connection, Statement statement) {
        doClose(null, connection, statement);
    }

    /**
     * 关闭连接
     * @param resultSet -
     * @param connection - 连接
     * @param statement -
     */
    public static void doClose(ResultSet resultSet, Connection connection, Statement statement) {
        try {
            if (resultSet != null){
                resultSet.close();
            }
        } catch (Exception e) {
            throw new CustomRuntimeException(ExceptionCodeEnum.RESULTSET_CLOSE_EXCEPTION, e);
        }
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (Exception e) {
            throw new CustomRuntimeException(ExceptionCodeEnum.CONNECTION_CLOSE_EXCEPTION, e);
        }
        try {
            if (statement != null) {
                statement.close();
            }
        } catch (SQLException e) {
            throw new CustomRuntimeException(ExceptionCodeEnum.STATEMENT_CLOSE_EXCEPTION, e);
        }
    }

}
